package easy;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution_859 {

    public boolean buddyStrings(String s, String goal) {
        if (s.length() != goal.length()) {
            return false;
        }
        Set<Character> set = new HashSet<>();
        char[] goals = goal.toCharArray();
        char[] chars = s.toCharArray();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < goals.length; i++) {
            if (chars[i] != goals[i]) {
                list.add(i);
                if (list.size() > 2) {
                    return false;
                }
            }
            set.add(goals[i]);
        }
        if (list.size() == 0) {
            return set.size() < goals.length;
        } else if (list.size() == 2 && chars[list.get(0)] == goals[list.get(1)] && chars[list.get(1)] == goals[list
            .get(0)]) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Solution_859 model = new Solution_859();
        System.out.println(model.buddyStrings("ab", "ba"));
        System.out.println(model.buddyStrings("ab", "ab"));
        System.out.println(model.buddyStrings("abb", "aab"));
    }
}
